using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ChipSharp
{
    public partial class MainForm : Form
    {
        private Game myGame = null;
        private Renderer myRenderer = null;
        private int myPressing = 0;

        public MainForm()
        {
            InitializeComponent();

            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);

        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.ResizeRedraw = true;   // automatically invalidating the window on resizing
            myRenderer = new Renderer(this, this.ClientRectangle.Width, this.ClientRectangle.Height);
            myGame = new Game(this, myRenderer);
        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
            myRenderer.Resize(this.ClientRectangle.Width, this.ClientRectangle.Height);
        }

        private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            myRenderer.Render(e);
        }

        private void MainForm_Paint(object sender, PaintEventArgs e)
        {
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            myGame.Abort();
        }

        private void MainForm_KeyDown(object sender, KeyEventArgs e)
        {

            switch (e.KeyValue)
            {
                case 37:
                    myGame.ManMove(World.Direction.kLeft);
                    break;
                case 38:
                    myGame.ManMove(World.Direction.kUp);
                    break;
                case 39:
                    myGame.ManMove(World.Direction.kRight);
                    break;
                case 40:
                    myGame.ManMove(World.Direction.kDown);
                    break;
            }
            myPressing = e.KeyValue;
        }

        private void MainForm_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyValue)
            {
                case 37:
                case 38:
                case 39:
                case 40:
                    if (e.KeyValue == myPressing)
                    {
                        myPressing = 0;
                        myGame.ManStop();
                    }
                    break;
            }
        }
    }
}